package ar.com.solidussource.howmuchservices.web.controller;

import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import ar.com.solidussource.framework.backend.bo.exception.BusinessException;
import ar.com.solidussource.framework.backend.dao.filter.Page;
import ar.com.solidussource.framework.backend.dao.filter.Result;
import ar.com.solidussource.framework.backend.dao.filter.TableOrder;
import ar.com.solidussource.howmuchservices.backend.bo.MarketCRUDBO;
import ar.com.solidussource.howmuchservices.backend.bo.ProductCRUDBO;
import ar.com.solidussource.howmuchservices.backend.bo.ProductpriceCRUDBO;
import ar.com.solidussource.howmuchservices.common.entities.Market;
import ar.com.solidussource.howmuchservices.common.entities.Product;
import ar.com.solidussource.howmuchservices.common.entities.Productprice;
import ar.com.solidussource.howmuchservices.common.entities.filter.ProductpriceFilter;
import ar.com.solidussource.howmuchservices.utils.json.JSONUtils;

@Controller
@RequestMapping("/productprice")
public class ProductpriceController extends BaseController {
	private static final Logger log = Logger.getLogger(ProductpriceController.class);
	private static final String TABLEORDER_CRITERION = "createdate";
	private static final String TABLEORDER_DIRECTION = "desc";
		
	@Autowired ProductpriceCRUDBO productpriceCRUDBO;
	@Autowired MarketCRUDBO marketCRUDBO;
	@Autowired ProductCRUDBO productCRUDBO;
	
	@RequestMapping(value="/show.json", method = RequestMethod.GET, produces="application/json")
	@ResponseBody
	public String getCurrentProductprice(HttpServletResponse httpServletResponse, @RequestParam Integer marketId, @RequestParam Integer productId) {
		
		try {
			Market market = marketCRUDBO.getById(marketId);
			
			if (market != null) {
				
				Product product = productCRUDBO.getById(productId);
				
				if (product != null) {
					ProductpriceFilter productpriceFilter = new ProductpriceFilter();
					productpriceFilter.setMarketId(marketId);
					productpriceFilter.setProductId(productId);
					
					productpriceFilter.setTableOrder(new TableOrder(TABLEORDER_CRITERION, TABLEORDER_DIRECTION));
					
					Page page = new Page(1, 1);
					
					Result<Productprice> productprices = productpriceCRUDBO.getAll(productpriceFilter, page);

					return JSONUtils.getJSON(productprices.getResult());					
					
				} else {
					throw new BusinessException("no product "+productId);							
				}				
			} else {
				throw new BusinessException("no market "+marketId);					
			}
			
		} catch (Exception e) {
			httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			log.error(e.getMessage(), e);			
			return getJsonStatus(e.getMessage());
		}
		
	}	
	
	@RequestMapping(value="/list.json", method = RequestMethod.GET, produces="application/json")
	@ResponseBody
	public String getProductprices(HttpServletResponse httpServletResponse, @RequestParam Integer productId, @RequestParam Integer pageNumber, @RequestParam Integer size) {
		
		try {			
			Product product = productCRUDBO.getById(productId);
				
			if (product != null) {
				ProductpriceFilter productpriceFilter = new ProductpriceFilter();
				productpriceFilter.setProductId(productId);
				
				productpriceFilter.setTableOrder(new TableOrder(TABLEORDER_CRITERION, TABLEORDER_DIRECTION));
					
				Page page = new Page(pageNumber, size);			
					
				Result<Productprice> productprices = productpriceCRUDBO.getAll(productpriceFilter, page);
					
				return JSONUtils.getJSON(productprices.getResult());
				
			} else {
				throw new BusinessException("no product "+productId);							
			}				
			 
			
		} catch (Exception e) {
			httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			log.error(e.getMessage(), e);			
			return getJsonStatus(e.getMessage());
		}
		
	}
	
	@RequestMapping(value="/score.json", method = RequestMethod.PUT, produces="application/json")
	@ResponseBody
	public String score(HttpServletResponse httpServletResponse, @RequestParam Integer productpriceId){
		
		try {
			Productprice productprice = productpriceCRUDBO.getById(productpriceId);
			
			if (productprice != null) {
				productprice.setScore(productprice.getScore() + 1);
				productpriceCRUDBO.saveOrUpdate(productprice);
				
				return getJsonStatus(RESPONSE_OK);	
				
			} else {
				throw new BusinessException("no productprice "+productpriceId);
			}
		} catch (Exception e) {
			httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			log.error(e.getMessage(), e);			
			return getJsonStatus(e.getMessage());
		}		
	}
	
	@RequestMapping(value="/unscore.json", method = RequestMethod.PUT, produces="application/json")
	@ResponseBody
	public String unscore(HttpServletResponse httpServletResponse, @RequestParam Integer productpriceId){
		
		try {
			Productprice productprice = productpriceCRUDBO.getById(productpriceId);
			
			if (productprice != null) {
				productprice.setScore(productprice.getScore() - 1);
				productpriceCRUDBO.saveOrUpdate(productprice);
				
				return getJsonStatus(RESPONSE_OK);	
				
			} else {
				throw new BusinessException("no productprice "+productpriceId);
			}
		} catch (Exception e) {
			httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			log.error(e.getMessage(), e);			
			return getJsonStatus(e.getMessage());
		}		
	}	
	
	@RequestMapping(value="/create.json", method = RequestMethod.PUT, produces="application/json")
	@ResponseBody
	public String createItemprice(HttpServletResponse httpServletResponse, @RequestParam Integer productId, @RequestParam Integer marketId, @RequestParam Float price){
		
		try {
			Product product = productCRUDBO.getById(productId);
			
			if (product != null) {
				Market market = marketCRUDBO.getById(marketId);
				
				if (market != null) {
					Productprice productprice = new Productprice();
					
					productprice.setProduct(product);
					productprice.setMarket(market);
					productprice.setPrice(price);
					
					productprice.setScore(0);
					productprice.setDeleted(false);
					productprice.setCreatedate(new Date());
					productprice.setCreateuser(getCurrentUser().getId());
					
					productpriceCRUDBO.saveNew(productprice);
					
					return getJsonStatus(RESPONSE_OK);
					
				} else {
					throw new BusinessException("no market "+marketId);
				}				
				
			} else {
				throw new BusinessException("no product "+productId);
			}
			
		} catch (Exception e) {
			httpServletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
			log.error(e.getMessage(), e);			
			return getJsonStatus(e.getMessage());
		}
		
	}
	

}
